<?php

namespace app\admin\controller\scanwork;

use app\common\controller\Backend;
use think\Db;

/**
 * 库存盘点
 * @icon fa fa-clipboard-check
 */
class StockInventory extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\scanwork\StockInventory;
    }

    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->where($where)->order($sort, $order)->paginate($limit);
            return json(["total" => $list->total(), "rows" => $list->items()]);
        }
        return $this->view->fetch();
    }

    /**
     * 盘点明细
     */
    public function detail($ids = null)
    {
        $inventory = $this->model->get($ids);
        if (!$inventory) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isAjax()) {
            $details = Db::name('scanwork_stock_inventory_detail')
                ->alias('d')
                ->join('scanwork_material m', 'd.material_id = m.id')
                ->where('d.inventory_id', $ids)
                ->field('d.*, m.material_name, m.material_code')
                ->select();
            return json(['code' => 1, 'data' => $details]);
        }

        $this->view->assign('inventory', $inventory);
        return $this->view->fetch();
    }
}

